package yja.gzw.app.activity

import android.util.Log
import android.widget.Toast
import com.bilibili.lib.accountoauth.OAuthManager
import com.bytedance.sdk.open.aweme.authorize.model.Authorization
import com.bytedance.sdk.open.douyin.DouYinOpenApiFactory
import com.kwai.auth.ILoginListener
import com.kwai.auth.KwaiAuthAPI
import com.kwai.auth.common.InternalResponse
import com.kwai.auth.common.KwaiConstants
import com.kwai.auth.login.kwailogin.KwaiAuthRequest
import yja.gzw.app.R
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityBasedingzhanghaoBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import yja.gzw.app.window.Chicun



class BasedingzhanghaoActivity : BaseActivity<Mainviewmodel, ActivityBasedingzhanghaoBinding>(
    R.layout.activity_basedingzhanghao){
    val chidun=Chicun()

    private lateinit var manager: OAuthManager

    override fun initView() {

        manager = OAuthManager(
            this,
            "3de45c3272784fbb",
            "yja.gzw.app"
        )
        manager.setOAuthCallback(object : OAuthManager.IOauthCallback {
            override fun onGetCode(code: String?, msg: String?) {
                Log.e("bilibili授权", "成功")
                Toast.makeText(this@BasedingzhanghaoActivity, "bilibili授权", Toast.LENGTH_SHORT).show()
            }

        })



        chidun.chicun(binding.douyingshouquan,100,40)
        chidun.chicun(binding.kuaishoushouquan,100,40)
        chidun.chicun(binding.tenxunshouquan,100,40)
        chidun.chicun(binding.xiguashouquan,100,40)
        chidun.chicun(binding.bilishouquan,100,40)
        binding.douyingshouquan.setOnClickListener {

            val douyinOpenApi = DouYinOpenApiFactory.create(this)
            val request = Authorization.Request()
            request.scope = "trial.whitelist,user_info"

            request.callerLocalEntry = "yja.gzw.app.DouYinEntryActivity"
            douyinOpenApi.authorize(request)



        }
        binding.xiguashouquan.setOnClickListener {
            val douyinOpenApi = DouYinOpenApiFactory.create(this)
            val request = Authorization.Request()
            request.scope = "trial.whitelist,user_info"

            request.callerLocalEntry = "yja.gzw.app.DouYinEntryActivity"
            douyinOpenApi.authorize(request)


        }
        binding.bilishouquan.setOnClickListener {

            UserData.bilishouquan=true
            manager.startOauth()
        }

            //todo 快手页面


        binding.kuaishoushouquan.setOnClickListener {
//
//            val klfwrt= Kuaishou()
//            klfwrt.mifnsf(this)


            val request = KwaiAuthRequest.Builder()
                .setState("state_str")
                .setRequiredScopes("user_info") // 必选权限，授权页无法手动取消勾选
                .setAuthMode(KwaiConstants.AuthMode.AUTHORIZE)
                .setLoginType(KwaiConstants.LoginType.APP)
                .setPlatformArray(
                    arrayOf(
                        KwaiConstants.Platform.KWAI_APP,
                        KwaiConstants.Platform.NEBULA_APP
                    )
                )
                .build()
            KwaiAuthAPI.getInstance().sendRequest(this, request,loginListener)

            

        }




    }

    override fun dataObservese() {

    }


    val loginListener: ILoginListener = object : ILoginListener {
        override fun onSuccess(response: InternalResponse) {
            // 根据授权返回的code请求openId(详细可以参考demo)。为了安全性，建议放在第三方的服务器中，由接入方服务器实现这个请求接口后将openid返回接入方的客户端。
            response.code

            // 根据授权返回的code请求openId(详细可以参考demo)。为了安全性，建议放在第三方的服务器中，由接入方服务器实现这个请求接口后将openid返回接入方的客户端。
            Log.e("快手授权", "token" + response.accessToken+"      code"+response.code)
            Log.e("快手授权", "快手已经授权成功")
            Toast.makeText(this@BasedingzhanghaoActivity, "快手已经授权成功", Toast.LENGTH_SHORT).show()
          UserData.Kuaishoushouquan=true

        }

        override fun onFailed(state: String, errCode: Int, errMsg: String) {
            Log.e("快手授权", "失败")
        }

        override fun onCancel() {
            Log.e("快手授权", "关闭")
        }
    }



}